به نام خداوند بخشاینده مهربان

تجزیه و تحلیل گراف شبکه همکاری آکادمیک با محوریت شناسایی انجمن های نهان

با استفاده از پایتون و نتورک ایکس

محمد حیدری

www.bigdataworld.ir

m_heydari@modares.ac.ir moh.heydari@mail.sbu.ac.ir heydari.mohammad8@gmail.com

1.png

نصب کتابخانه نتورک ایکس

In [ ]:
#pip install networkx

نسخه نتورک ایکس

In [254]:
print('NetworkX version: {}'.format(nx.__version__))
NetworkX version: 2.2

600_439817972.png

In [265]:
from IPython.display import Image

ایمپورت کتابخانه های ضروری

In [232]:
import collections
import itertools
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
from networkx.algorithms.community import k_clique_communities, girvan_newman
from networkx.algorithms import community
import community
import numpy as np
import pandas as pd
import scipy.spatial as spt
%matplotlib inline

from networkx.algorithms import bipartite

Data Structures: Graphs, DiGraphs, MultiGraphs and MultiDiGraphs

  • Graph: Undirected graph, allows self-loops

  • DiGraph: Directed graph, allows self-loops

  • MultiGraph: Undirected graph with parallel edges, allows self-loops

  • MultiDiGraph: Directed graph with parallel edges, allows self-loops

In [256]:
G = nx.Graph()
D = nx.DiGraph()
MG = nx.MultiGraph()
MDG = nx.MultiDiGraph()

نمایش یک گراف ابتدایی

In [261]:
G = nx.Graph()
G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'C')])
nx.draw(G, node_size=500, node_color='green', with_labels=True)
C:\ProgramData\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):

ماتریس های گراف

ماتریس همسایگی یال‌های میان گره‌های گراف را نمایش می دهد

درایه ماتریس نشان می دهد آیا بین دو گره الف و ب یالی برقرار است یا خیر

در ماتریس همسایگی اگر گراف طوقه نداشته باشد درایه های قطر اصلی همگی صفر هستند

adj_matrix.png

Adjacency Matrix A $n x n$ matrix:

    0  1  1        G = [[0, 1, 1],
    1  0  1             [1, 0, 1],
    1  1  0             [1, 1, 0]]

Adjacency List A list of neighbors:

    A: B, C        G = {'A': ['B', 'C'],
    B: A, C             'B': ['A', 'C'],
    C: A, B             'C': ['A', 'B']} 

Edge List A list of edges:

    A B            G = [['A', 'B'],
    A C                 ['A', 'C'],
    B C                 ['B', 'C']]

NetworkX uses a dictionary of dictionaries based Adjacency List format which is fast and ligthweight for sparse graphs. This approach is inspired on Guido van Rossum's essay Python Patterns - Implementing Graphs and in the work of David Eppstein on Python Algorithms and Data Structures.

This approach allows for natural expressions such as:

  • n in G to test if the graph $G$ contains node $n$
  • for n in G to loop over all nodes
  • G[n] to access all neighbors of $n$ in $G$
  • len(G) to get the number of nodes in $G$

Internally the node $n$ is a key in the $G.adj$ dictionary, values are themselves dictionaries with neighbors as keys and another dictionary as value that holds edge attributes.

So NetworkX graphs are "dictionaries all the way down". This is not exactly true in version 2.0 but it is safe for users to think of it this way.

In [267]:
print(G.adj)
{'A': {'B': {}, 'C': {}}, 'B': {'A': {}, 'C': {}}, 'C': {'A': {}, 'B': {}}}

چک موجودیت یک نود در گراف

In [268]:
'A' in G
Out[268]:
True

چاپ نودهای گراف با استفاده از حلقه دوست داشتنی

In [271]:
for node in G:
    print(node)
A
B
C

نمایش تعداد آیتم ها در گراف

In [272]:
len(G)
Out[272]:
3
In [283]:
# Create an undirected Graph
G = nx.Graph()
# One node at a time
G.add_node(1)  # "method" of G
# A list of nodes
G.add_nodes_from([2, 3])
# A container of nodes
H = nx.path_graph(10)
G.add_nodes_from(H) # G now contains the nodes of H
# In contrast, you could use the graph H as a node in G. 
G.add_node(H) # G now contains Graph H as a node 
In [284]:
nx.draw(G, with_labels=True, node_color='green')
In [285]:
# Adding a single edge
G.add_edge(1, 2)
# Add a list of edges 
G.add_edges_from([(1, 2), (1, 3)])
# Add from a container of edges
G.add_edges_from(H.edges())
In [286]:
nx.draw(G, with_labels=True, node_color='green')
C:\ProgramData\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):

آپلود فایل دیتاست در صورتی که بخواهیم در (کُلَب) کار کنیم

In [ ]:
from google.colab import files
up = files.upload()

خواندن فایل دیتاست و ساخت دیتافریم

In [29]:
df = pd.read_excel('the_data.xlsx', columns= ['source', 'target'])

محتوی دیتافریم

In [31]:
df.head()
Out[31]:
source target
0 Tehran University of Medical Science University of Tehran
1 Tehran University of Medical Science Tarbiat Modares university
2 University of Tehran Amirkabir University of Technology
3 University of Tehran IAU
4 Tarbiat Modares university Tarbiat Modares university

ساخت گراف

In [30]:
g = nx.from_pandas_edgelist(df,'source','target')

اطلاعات ساختاری گراف

میانگین درجه

هر گره به طور میانگین با چند گره دیگر در ارتباط است

In [181]:
print(nx.info(g))
Name: 
Type: Graph
Number of nodes: 183
Number of edges: 320
Average degree:   3.4973

نظریه 6 درجه جدایی

In [198]:
print(nx.diameter(g))
7

بررسی همبندبودن شبکه

In [204]:
print(nx.is_connected(g))
True

نمایش نودهای گراف

In [184]:
g.nodes
Out[184]:
NodeView(('Tehran University of Medical Science', 'University of Tehran', 'Tarbiat Modares university', 'Amirkabir University of Technology', 'IAU ', 'Isfahan University of Technology', 'Iran University of Science and Technology', 'University of Applied Science and Technology', 'Ferdowsi University of Mashhad', 'Mashhad University of Medical Sciences', 'Shiraz University', 'University of Guilan', 'Tabriz University of Medical Sciences', 'University of Tabriz', 'Shahid Beheshti University', 'University of Isfahan', 'Isfahan University of Medical Sciences', 'Shahid Beheshti University of Medical Sciences and Health Services', 'Iran University of Medical Sciences', 'Sharif University of Technology', 'Shiraz University of Medical Sciences', 'Shahid Rajaee Teacher Training University', "Allameh Tabataba'i University", 'Urmia University', 'Yazd University', 'Shahid Sadoughi University of Medical Sciences', 'Semnan University', 'Semnan University of Medical Sciences', 'Shahid Chamran University of Ahvaz', 'Ahvaz Jundishapur University of Medical Sciences', 'University of Mohaghegh Ardabili', 'Ardabil University of Medical Sciences', 'Shahid Bahonar University of Kerman Kerman', 'Kerman University of Medical Sciences', 'International Center for Science, High Technology & Environmental Sciences', 'Persian Gulf University ', 'Payame Noor University', 'Kharazmi University ', 'University of Sistan and Baluchestan', 'Uniersity of Tehran', 'Zahedan University of Medical Sciences', 'Razi University', 'Kermanshah University of Medical Sciences', 'University of Birjand', 'Birjand University of Technology', 'Birjand University of Medical Sciences', 'University of Mazandaran', 'Mazandaran University of Medical Sciences', 'Imam Khomeini International University', 'Qazvin University of Medical Sciences', 'Shahrekord University of Medical Sciences', 'Shahrekord University', 'Technical and Vocational University of Iran', 'Institute for Advanced Studies in Basic Sciences (IASBS)', 'University of Zanjan', 'Zanjan University of Medical Sciences', 'Shahrood University of Technology', 'Shahroud University of Medical Sciences', 'Alzahra University', 'Zabol University of Medical Sciences', 'Zabol University', 'Shahed University', 'Malek-Ashtar University of Technology', 'Lorestan University', 'Lorestan University of Medical Sciences', 'Qom University of Medical Sciences and Health Services', 'University of Qom', 'Qom University of Technology', 'Babol University of Medical Sciences', 'Babol Noshirvani University of Technology', 'University of Kurdistan ', 'Azarbaijan Shahid Madani University', 'Arak University ', 'Arak University of Technology', 'Arak University of Medical Sciences', 'University of Kashan', 'Kashan University of Medical Sciences', 'Imam Hossein University', 'Imam Sadiq University', 'Kahramanmaras Sutcu Imam University', 'Imam Reza International University', 'Alborza University of Medical Science', 'Saveh University of Medical Science', 'Iranian Research Institute for Information Science and Technology', 'Iran Telecommunication Research Center - ITRC', 'Atomic Energy Organization of Iran', 'Institute for Research in Fundamental Sciences - IPM', 'Royan Institute', 'Academic Center for Education, Culture and Research', 'Urmia University of Medical Sciences', 'Malayer University', 'Bu-Ali Sina University', 'University of Ilam', 'Ilam University of Medical Sciences', 'Hormozgan University', 'Hormozgan University of Medical Sciences', 'Vali-e-Asr University of Rafsanjan', 'Shahid Bahonar University of Kerman', 'Kerman Graduate University of Technology', 'Kerman University of Medical Sciences ', 'Golestan University', 'Golestan University of Medical Sciences ', 'Damghan University', 'Jiroft University of Medical Sciences ', 'Petroleum University of Technology', 'Ardakan University', 'University of Bojnord', 'Sahand University of Technology', 'Maragheh University', 'Maragheh University of Medical Sciences ', 'Sirjan University of Technology', 'Shiraz University of Technology', 'Urmia University of Technology', 'Fasa University of Medical Sciences ', 'Shiraz University of Medical Sciences ', 'Tafresh University', 'Bonan University', 'Khatam Institute of Higher Education', 'Hamedan University of Technology', 'University of Neyshabur', 'Neyshabur University of Medical Sciences ', 'University of Sabzevar', 'IAU  of Yazd', 'University College of Nabi Akram', 'Tabriz Islamic Art University ', 'Institute for Color Science and Technology ', 'IAU Tabriz Branch Tabriz', 'IAU , Zahedan Branch', 'IAU Semnan Branch ', 'IAU , Garmsar Branch', 'IAU , Qom', 'IAU , Science and Research Branch, Tehran', 'Shomal University ', 'IAU - Tonekabon Branch Tonekābon', 'IAU , Sari ', 'IAU of Tafresh', 'IAU - Saveh Branch', 'IAU Khomeini Shahr', 'IAU , Khorramabad Branch', 'IAU Borujerd Branch', 'Kurdistan University of Medical Sciences', 'IAU of Sanandaj', 'University of Kurdistan', 'Yasuj University of Medical Sciences', 'University of Yasuj', 'IAU , Gachsaran', 'IAU , Mahshahr Branch Bandar Emam Khomeyni', 'Aliabad Katoul IAU ', 'IAU Tehran Science and Research Branch', 'IAU , Birjand Branch', 'IAU Kermanshah Branch', 'Rafsanjan University of Medical Sciences', 'Art University of Isfahan ', 'Universität Siegen', 'IAU of Hamedan', 'Gorgan University of Agricultural Sciences and Natural Resources', 'Fasa University', 'Universite des Sciences et Techniques de Masuku', 'IAU Shahrekord Branch', 'Tehran Art University ', 'IAU Karaj Branch', 'Quchan University of Technology', 'Sadjad Institute of Higher Education', 'Deakin University', 'Torbat Heydarieh University of Medical Sciences ', 'University of Torbat Heydarieh Torbat-e Ḩeydarīyeh', 'Civil Aviation Technology College ', 'Baqiyatallah University of Medical Sciences', 'Islamic Republic of Iran Broadcasting University', 'Central Bank of the Islamic Republic of Iran', 'Iran Polymer and Petrochemical Institute', 'University of Science and Culture', 'Royesh Stem Cell Biotechnology Institute', 'IAU Tehran North Branch ', 'IAU Tehran Medical Branch ', 'IAU Dental Branch of Tehran', 'IAU South Tehran Branch ', 'IAU East Tehran Branch ', 'University of Social Welfare and Rehabilitation Sciences', 'Iran University of Medical Science', 'University of Economic Sciences', 'Power and Water University of Technology', 'Khaje Nasir Toosi University of Technology '))

نمایش یال های گراف

In [182]:
g.edges
Out[182]:
EdgeView([('Tehran University of Medical Science', 'University of Tehran'), ('Tehran University of Medical Science', 'Tarbiat Modares university'), ('Tehran University of Medical Science', 'Shahid Beheshti University of Medical Sciences and Health Services'), ('Tehran University of Medical Science', 'Iran University of Medical Sciences'), ('Tehran University of Medical Science', 'Isfahan University of Medical Sciences'), ('Tehran University of Medical Science', 'Shiraz University of Medical Sciences'), ('Tehran University of Medical Science', 'Mashhad University of Medical Sciences'), ('Tehran University of Medical Science', 'Shahid Sadoughi University of Medical Sciences'), ('Tehran University of Medical Science', 'Semnan University of Medical Sciences'), ('Tehran University of Medical Science', 'Ahvaz Jundishapur University of Medical Sciences'), ('Tehran University of Medical Science', 'Ardabil University of Medical Sciences'), ('Tehran University of Medical Science', 'Kerman University of Medical Sciences'), ('Tehran University of Medical Science', 'Zahedan University of Medical Sciences'), ('Tehran University of Medical Science', 'Birjand University of Medical Sciences'), ('Tehran University of Medical Science', 'Mazandaran University of Medical Sciences'), ('Tehran University of Medical Science', 'Imam Khomeini International University'), ('Tehran University of Medical Science', 'Qazvin University of Medical Sciences'), ('Tehran University of Medical Science', 'Shahrekord University of Medical Sciences'), ('Tehran University of Medical Science', 'Zanjan University of Medical Sciences'), ('Tehran University of Medical Science', 'Shahroud University of Medical Sciences'), ('Tehran University of Medical Science', 'Zabol University of Medical Sciences'), ('Tehran University of Medical Science', 'Shahed University'), ('Tehran University of Medical Science', 'Lorestan University of Medical Sciences'), ('Tehran University of Medical Science', 'Qom University of Medical Sciences and Health Services'), ('Tehran University of Medical Science', 'Babol University of Medical Sciences'), ('Tehran University of Medical Science', 'Arak University of Medical Sciences'), ('Tehran University of Medical Science', 'Kashan University of Medical Sciences'), ('Tehran University of Medical Science', 'Alborza University of Medical Science'), ('Tehran University of Medical Science', 'Saveh University of Medical Science'), ('Tehran University of Medical Science', 'Atomic Energy Organization of Iran'), ('Tehran University of Medical Science', 'Royan Institute'), ('Tehran University of Medical Science', 'Academic Center for Education, Culture and Research'), ('Tehran University of Medical Science', 'Urmia University of Medical Sciences'), ('Tehran University of Medical Science', 'Ilam University of Medical Sciences'), ('Tehran University of Medical Science', 'Hormozgan University of Medical Sciences'), ('Tehran University of Medical Science', 'Vali-e-Asr University of Rafsanjan'), ('Tehran University of Medical Science', 'Kerman University of Medical Sciences '), ('Tehran University of Medical Science', 'Golestan University of Medical Sciences '), ('Tehran University of Medical Science', 'Jiroft University of Medical Sciences '), ('Tehran University of Medical Science', 'Maragheh University'), ('Tehran University of Medical Science', 'Maragheh University of Medical Sciences '), ('Tehran University of Medical Science', 'Fasa University of Medical Sciences '), ('Tehran University of Medical Science', 'Neyshabur University of Medical Sciences '), ('Tehran University of Medical Science', 'University of Sabzevar'), ('Tehran University of Medical Science', 'IAU - Tonekabon Branch Tonekābon'), ('Tehran University of Medical Science', 'Kurdistan University of Medical Sciences'), ('Tehran University of Medical Science', 'Yasuj University of Medical Sciences'), ('Tehran University of Medical Science', 'Rafsanjan University of Medical Sciences'), ('Tehran University of Medical Science', 'Baqiyatallah University of Medical Sciences'), ('Tehran University of Medical Science', 'IAU Tehran Medical Branch '), ('Tehran University of Medical Science', 'University of Social Welfare and Rehabilitation Sciences'), ('University of Tehran', 'Amirkabir University of Technology'), ('University of Tehran', 'IAU '), ('University of Tehran', 'Iran University of Science and Technology'), ('University of Tehran', 'Isfahan University of Technology'), ('University of Tehran', 'Ferdowsi University of Mashhad'), ('University of Tehran', 'Shiraz University'), ('University of Tehran', 'University of Guilan'), ('University of Tehran', 'Tabriz University of Medical Sciences'), ('University of Tehran', 'Shahid Beheshti University'), ('University of Tehran', 'Sharif University of Technology'), ('University of Tehran', 'Shahid Rajaee Teacher Training University'), ('University of Tehran', "Allameh Tabataba'i University"), ('University of Tehran', 'University of Tabriz'), ('University of Tehran', 'Semnan University'), ('University of Tehran', 'Payame Noor University'), ('University of Tehran', 'Kharazmi University '), ('University of Tehran', 'University of Birjand'), ('University of Tehran', 'University of Mazandaran'), ('University of Tehran', 'Shahrekord University'), ('University of Tehran', 'Technical and Vocational University of Iran'), ('University of Tehran', 'Institute for Advanced Studies in Basic Sciences (IASBS)'), ('University of Tehran', 'University of Zanjan'), ('University of Tehran', 'Shahrood University of Technology'), ('University of Tehran', 'Alzahra University'), ('University of Tehran', 'Zabol University'), ('University of Tehran', 'Malek-Ashtar University of Technology'), ('University of Tehran', 'Lorestan University'), ('University of Tehran', 'University of Qom'), ('University of Tehran', 'Babol Noshirvani University of Technology'), ('University of Tehran', 'University of Kurdistan '), ('University of Tehran', 'Arak University '), ('University of Tehran', 'University of Kashan'), ('University of Tehran', 'Imam Hossein University'), ('University of Tehran', 'Imam Sadiq University'), ('University of Tehran', 'Iranian Research Institute for Information Science and Technology'), ('University of Tehran', 'Atomic Energy Organization of Iran'), ('University of Tehran', 'Institute for Research in Fundamental Sciences - IPM'), ('University of Tehran', 'Bu-Ali Sina University'), ('University of Tehran', 'University of Ilam'), ('University of Tehran', 'Hormozgan University'), ('University of Tehran', 'Golestan University'), ('University of Tehran', 'Damghan University'), ('University of Tehran', 'Sahand University of Technology'), ('University of Tehran', 'Tafresh University'), ('University of Tehran', 'Hamedan University of Technology'), ('University of Tehran', 'Institute for Color Science and Technology '), ('University of Tehran', 'IAU , Science and Research Branch, Tehran'), ('University of Tehran', 'IAU of Tafresh'), ('University of Tehran', 'Art University of Isfahan '), ('University of Tehran', 'Gorgan University of Agricultural Sciences and Natural Resources'), ('University of Tehran', 'Tehran Art University '), ('University of Tehran', 'IAU Karaj Branch'), ('University of Tehran', 'Civil Aviation Technology College '), ('University of Tehran', 'Islamic Republic of Iran Broadcasting University'), ('University of Tehran', 'Central Bank of the Islamic Republic of Iran'), ('University of Tehran', 'Iran Polymer and Petrochemical Institute'), ('University of Tehran', 'IAU Tehran North Branch '), ('University of Tehran', 'IAU South Tehran Branch '), ('University of Tehran', 'IAU East Tehran Branch '), ('University of Tehran', 'University of Economic Sciences'), ('University of Tehran', 'Power and Water University of Technology'), ('University of Tehran', 'Khaje Nasir Toosi University of Technology '), ('Tarbiat Modares university', 'Tarbiat Modares university'), ('Tarbiat Modares university', 'Isfahan University of Technology'), ('Tarbiat Modares university', 'Razi University'), ('Tarbiat Modares university', 'Zabol University'), ('Tarbiat Modares university', 'Iranian Research Institute for Information Science and Technology'), ('Tarbiat Modares university', 'Iran Telecommunication Research Center - ITRC'), ('Tarbiat Modares university', 'IAU Borujerd Branch'), ('Tarbiat Modares university', 'Tehran Art University '), ('Tarbiat Modares university', 'Central Bank of the Islamic Republic of Iran'), ('Tarbiat Modares university', 'Power and Water University of Technology'), ('Amirkabir University of Technology', 'IAU '), ('Amirkabir University of Technology', 'Qom University of Technology'), ('Amirkabir University of Technology', 'Tafresh University'), ('Amirkabir University of Technology', 'Institute for Color Science and Technology '), ('Amirkabir University of Technology', 'Shomal University '), ('Amirkabir University of Technology', 'IAU , Birjand Branch'), ('IAU ', 'University of Applied Science and Technology'), ('IAU ', 'Isfahan University of Technology'), ('IAU ', 'University of Guilan'), ('IAU ', 'Shahid Beheshti University'), ('IAU ', 'Sharif University of Technology'), ('IAU ', 'Shahid Rajaee Teacher Training University'), ('IAU ', "Allameh Tabataba'i University"), ('IAU ', 'Urmia University'), ('IAU ', 'Yazd University'), ('IAU ', 'Semnan University'), ('IAU ', 'Shahid Chamran University of Ahvaz'), ('IAU ', 'University of Mohaghegh Ardabili'), ('IAU ', 'Payame Noor University'), ('IAU ', 'Kharazmi University '), ('IAU ', 'University of Mazandaran'), ('IAU ', 'Imam Khomeini International University'), ('IAU ', 'Shahrekord University'), ('IAU ', 'Technical and Vocational University of Iran'), ('IAU ', 'University of Zanjan'), ('IAU ', 'Shahrood University of Technology'), ('IAU ', 'Alzahra University'), ('IAU ', 'Shahed University'), ('IAU ', 'Malek-Ashtar University of Technology'), ('IAU ', 'Lorestan University'), ('IAU ', 'University of Qom'), ('IAU ', 'Qom University of Technology'), ('IAU ', 'University of Kurdistan '), ('IAU ', 'Arak University '), ('IAU ', 'University of Kashan'), ('IAU ', 'Imam Hossein University'), ('IAU ', 'Iran Telecommunication Research Center - ITRC'), ('IAU ', 'Malayer University'), ('IAU ', 'Bu-Ali Sina University'), ('IAU ', 'University of Ilam'), ('IAU ', 'Hormozgan University'), ('IAU ', 'Golestan University'), ('IAU ', 'Petroleum University of Technology'), ('IAU ', 'University of Bojnord'), ('IAU ', 'Sirjan University of Technology'), ('IAU ', 'Shiraz University of Technology'), ('IAU ', 'Bonan University'), ('IAU ', 'Khatam Institute of Higher Education'), ('IAU ', 'IAU - Saveh Branch'), ('IAU ', 'IAU , Khorramabad Branch'), ('IAU ', 'IAU Borujerd Branch'), ('IAU ', 'IAU of Sanandaj'), ('IAU ', 'University of Yasuj'), ('IAU ', 'IAU , Gachsaran'), ('IAU ', 'IAU , Mahshahr Branch Bandar Emam Khomeyni'), ('IAU ', 'Aliabad Katoul IAU '), ('IAU ', 'IAU , Birjand Branch'), ('IAU ', 'IAU Kermanshah Branch'), ('IAU ', 'IAU of Hamedan'), ('IAU ', 'Gorgan University of Agricultural Sciences and Natural Resources'), ('IAU ', 'IAU Shahrekord Branch'), ('IAU ', 'IAU Karaj Branch'), ('IAU ', 'Iran Polymer and Petrochemical Institute'), ('IAU ', 'IAU Tehran North Branch '), ('IAU ', 'IAU Tehran Medical Branch '), ('IAU ', 'IAU Dental Branch of Tehran'), ('IAU ', 'IAU South Tehran Branch '), ('IAU ', 'IAU East Tehran Branch '), ('IAU ', 'University of Economic Sciences'), ('IAU ', 'Khaje Nasir Toosi University of Technology '), ('Isfahan University of Technology', 'University of Isfahan'), ('Isfahan University of Technology', 'Yazd University'), ('Isfahan University of Technology', 'Ardakan University'), ('Iran University of Science and Technology', 'University of Applied Science and Technology'), ('Iran University of Science and Technology', 'Arak University of Technology'), ('Iran University of Science and Technology', 'Quchan University of Technology'), ('Iran University of Science and Technology', 'Torbat Heydarieh University of Medical Sciences '), ('Ferdowsi University of Mashhad', 'Mashhad University of Medical Sciences'), ('Ferdowsi University of Mashhad', 'University of Sistan and Baluchestan'), ('Ferdowsi University of Mashhad', 'University of Birjand'), ('Ferdowsi University of Mashhad', 'Imam Reza International University'), ('Ferdowsi University of Mashhad', 'Damghan University'), ('Ferdowsi University of Mashhad', 'University of Bojnord'), ('Ferdowsi University of Mashhad', 'University of Neyshabur'), ('Ferdowsi University of Mashhad', 'Quchan University of Technology'), ('Ferdowsi University of Mashhad', 'Sadjad Institute of Higher Education'), ('Ferdowsi University of Mashhad', 'University of Torbat Heydarieh Torbat-e Ḩeydarīyeh'), ('Mashhad University of Medical Sciences', 'Birjand University of Medical Sciences'), ('Mashhad University of Medical Sciences', 'Imam Reza International University'), ('Mashhad University of Medical Sciences', 'University of Neyshabur'), ('Mashhad University of Medical Sciences', 'Neyshabur University of Medical Sciences '), ('Mashhad University of Medical Sciences', 'University of Sabzevar'), ('Mashhad University of Medical Sciences', 'Torbat Heydarieh University of Medical Sciences '), ('Shiraz University', 'University of Guilan'), ('Shiraz University', 'Shiraz University of Medical Sciences'), ('Shiraz University', 'Persian Gulf University '), ('Shiraz University', 'Shiraz University of Technology'), ('Shiraz University', 'Fasa University'), ('Tabriz University of Medical Sciences', 'University of Tabriz'), ('Tabriz University of Medical Sciences', 'Ardabil University of Medical Sciences'), ('Tabriz University of Medical Sciences', 'Urmia University of Medical Sciences'), ('Tabriz University of Medical Sciences', 'Maragheh University of Medical Sciences '), ('Tabriz University of Medical Sciences', 'Khatam Institute of Higher Education'), ('University of Tabriz', 'Urmia University'), ('University of Tabriz', 'University of Mohaghegh Ardabili'), ('University of Tabriz', 'Azarbaijan Shahid Madani University'), ('University of Tabriz', 'Sahand University of Technology'), ('University of Tabriz', 'Urmia University of Technology'), ('University of Tabriz', 'Bonan University'), ('University of Tabriz', 'University College of Nabi Akram'), ('University of Tabriz', 'Tabriz Islamic Art University '), ('University of Tabriz', 'IAU Tabriz Branch Tabriz'), ('University of Isfahan', 'Isfahan University of Medical Sciences'), ('Isfahan University of Medical Sciences', 'Iran University of Medical Sciences'), ('Isfahan University of Medical Sciences', 'Shahrekord University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Iran University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Shahid Sadoughi University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Semnan University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Kerman University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Mazandaran University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Qazvin University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Zanjan University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Zabol University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Lorestan University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Arak University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Ilam University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Hormozgan University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Kerman University of Medical Sciences '), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Golestan University of Medical Sciences '), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Maragheh University'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Kurdistan University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'Baqiyatallah University of Medical Sciences'), ('Shahid Beheshti University of Medical Sciences and Health Services', 'IAU Dental Branch of Tehran'), ('Iran University of Medical Sciences', 'Ahvaz Jundishapur University of Medical Sciences'), ('Iran University of Medical Sciences', 'Zahedan University of Medical Sciences'), ('Iran University of Medical Sciences', 'Shahroud University of Medical Sciences'), ('Iran University of Medical Sciences', 'Qom University of Medical Sciences and Health Services'), ('Iran University of Medical Sciences', 'Kashan University of Medical Sciences'), ('Iran University of Medical Sciences', 'Alborza University of Medical Science'), ('Iran University of Medical Sciences', 'Saveh University of Medical Science'), ('Sharif University of Technology', 'Institute for Research in Fundamental Sciences - IPM'), ('Sharif University of Technology', 'Petroleum University of Technology'), ('Sharif University of Technology', 'Civil Aviation Technology College '), ('Urmia University', 'Urmia University of Technology'), ('Yazd University', 'Ardakan University'), ('Shahid Sadoughi University of Medical Sciences', 'IAU  of Yazd'), ('Semnan University', 'IAU Semnan Branch '), ('Shahid Chamran University of Ahvaz', 'Ahvaz Jundishapur University of Medical Sciences'), ('Shahid Bahonar University of Kerman Kerman', 'Kerman University of Medical Sciences'), ('Shahid Bahonar University of Kerman Kerman', 'International Center for Science, High Technology & Environmental Sciences'), ('Kerman University of Medical Sciences', 'Rafsanjan University of Medical Sciences'), ('International Center for Science, High Technology & Environmental Sciences', 'Shahid Bahonar University of Kerman'), ('International Center for Science, High Technology & Environmental Sciences', 'Kerman Graduate University of Technology'), ('Persian Gulf University ', 'Payame Noor University'), ('Payame Noor University', 'Azarbaijan Shahid Madani University'), ('Payame Noor University', 'University of Torbat Heydarieh Torbat-e Ḩeydarīyeh'), ('University of Sistan and Baluchestan', 'Uniersity of Tehran'), ('University of Sistan and Baluchestan', 'Birjand University of Technology'), ('University of Sistan and Baluchestan', 'IAU , Zahedan Branch'), ('Razi University', 'Kermanshah University of Medical Sciences'), ('Razi University', 'IAU Kermanshah Branch'), ('University of Birjand', 'Birjand University of Technology'), ('University of Mazandaran', 'Babol Noshirvani University of Technology'), ('Mazandaran University of Medical Sciences', 'Babol University of Medical Sciences'), ('Shahrekord University of Medical Sciences', 'IAU Shahrekord Branch'), ('Shahrekord University', 'IAU Khomeini Shahr'), ('Institute for Advanced Studies in Basic Sciences (IASBS)', 'University of Zanjan'), ('Shahrood University of Technology', 'IAU , Garmsar Branch'), ('Lorestan University', 'IAU , Khorramabad Branch'), ('Babol Noshirvani University of Technology', 'Shomal University '), ('Babol Noshirvani University of Technology', 'IAU , Sari '), ('Arak University ', 'Arak University of Technology'), ('Imam Sadiq University', 'Kahramanmaras Sutcu Imam University'), ('Royan Institute', 'Academic Center for Education, Culture and Research'), ('Royan Institute', 'University of Science and Culture'), ('Royan Institute', 'Royesh Stem Cell Biotechnology Institute'), ('Malayer University', 'Bu-Ali Sina University'), ('Bu-Ali Sina University', 'Hamedan University of Technology'), ('Bu-Ali Sina University', 'IAU of Hamedan'), ('Vali-e-Asr University of Rafsanjan', 'Shahid Bahonar University of Kerman'), ('Shahid Bahonar University of Kerman', 'Kerman Graduate University of Technology'), ('Shahid Bahonar University of Kerman', 'Sirjan University of Technology'), ('Kerman University of Medical Sciences ', 'Jiroft University of Medical Sciences '), ('Fasa University of Medical Sciences ', 'Shiraz University of Medical Sciences '), ('Tafresh University', 'IAU of Tafresh'), ('Tabriz Islamic Art University ', 'Institute for Color Science and Technology '), ('IAU , Qom', 'IAU , Science and Research Branch, Tehran'), ('IAU of Sanandaj', 'University of Kurdistan'), ('Yasuj University of Medical Sciences', 'University of Yasuj'), ('University of Yasuj', 'IAU , Gachsaran'), ('Aliabad Katoul IAU ', 'IAU Tehran Science and Research Branch'), ('Art University of Isfahan ', 'Universität Siegen'), ('Fasa University', 'Universite des Sciences et Techniques de Masuku'), ('Sadjad Institute of Higher Education', 'Deakin University'), ('Islamic Republic of Iran Broadcasting University', 'Central Bank of the Islamic Republic of Iran'), ('University of Science and Culture', 'Royesh Stem Cell Biotechnology Institute'), ('University of Social Welfare and Rehabilitation Sciences', 'Iran University of Medical Science')])

گره با بیشترین درجه در گراف

In [201]:
deg = dict(g.degree())
maxDeg = max(degrees.values())
maxDegKey = [i for i in degrees.keys() if degrees[i] == maxDeg]
print(maxDeg)
print(maxDegKey)
66
['IAU ']
In [203]:
plt.figure(figsize = (15,15))

pos = nx.kamada_kawai_layout(g)
node_colors = [ g.degree(n) for n in g ]
node_size = [30000*nx.betweenness_centrality(g)[n] for n in g]
nx.draw(g,pos = pos,node_color=node_colors, node_size=node_size,with_label = False,
                edge_color='0.6',alpha = 1,width = 0.3, cmap = plt.cm.Blues_r )

plt.axis('off')
#plt.tight_layout()
plt.show()
C:\ProgramData\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):

شناسایی گره های کلیدی و حیاتی در گراف با تکیه بر تئوری مرکزیت در شبکه

نحوه اتصال یک نود به نودهای دیگر در یک شبکه اجتماعی میتواند اطلاعاتی راجع به مهم بودن و یا مهم نبودن آن نود در کاربردهای خاص مشخص نماید

#

مثال میتوانیم مشخص کنیم کدام نود در انتشار شایعه بیشترین تاثیر را در یک شبکه اجتماعی دارد

#

بینابینی یک نود خاص در شبکه عبارت است از تعداد کوتاهترین مسیرهای میان نودهای شبکه که از یک نود خاص رد میشوند

#

در حقیقت این معیار محاسبه میکند چه تعداد از نودهای شبکه برای ارتباط سریعتر با هم (با واسطه کمتر) به این نود نیاز دارند

هر چه بینابینی نود زیادتر باشد یعنی اینکه نود در مکان استراتژیک تری قرار گرفته است

#

نزدیکی عبارت است از عکس متوسط فاصله یک نود تا نودهای دیگر گراف

نودی که دارای بیشترین مقدار نزدیکی است سرعت دسترسی بیشتری به نودهای دیگر دارد و

#

میتواند در مدت زمان کمی به همه نودها اطلاعات ارسال نماید یا از آنها اطلاعات بگیرد

In [217]:
degCent = nx.degree_centrality(g)
closeCent = nx.closeness_centrality(g)
betCent = nx.betweenness_centrality(g)
pageCent  = nx.pagerank(g)

# یک تابع تعریف می کنیم که معیارهای مرکزیت به دست آمده را به صورت مرتب شده به ما بازگرداند
def impNodes(centrality,n = 5):
    sortedCent = sorted(centrality.items(), key=lambda x:x[1], reverse=True)[:n]
    return sortedCent

print('مرکزیت درجه')
pprint(impNodes(degCent))
print('\n')

print('مرکزیت نزدیکی')
pprint(impNodes(closeCent))
print('\n')

print('مرکزیت بینابینی')
pprint(impNodes(betCent))
print('\n')

print('مرکزیت پیج رنک')
pprint(impNodes(pageCent))
print('\n')
مرکزیت درجه
[('IAU ', 0.3626373626373627),
 ('University of Tehran', 0.3461538461538462),
 ('Tehran University of Medical Science', 0.2802197802197802),
 ('Shahid Beheshti University of Medical Sciences and Health Services',
  0.1043956043956044),
 ('Tarbiat Modares university', 0.06593406593406594)]


مرکزیت نزدیکی
[('University of Tehran', 0.5669781931464174),
 ('Tehran University of Medical Science', 0.49056603773584906),
 ('IAU ', 0.4827586206896552),
 ('Atomic Energy Organization of Iran', 0.4126984126984127),
 ('Imam Khomeini International University', 0.40176600441501104)]


مرکزیت بینابینی
[('University of Tehran', 0.5328671986210572),
 ('Tehran University of Medical Science', 0.44573932647555525),
 ('IAU ', 0.3082803670789112),
 ('Ferdowsi University of Mashhad', 0.08267370607855802),
 ('University of Tabriz', 0.044545310686960694)]


مرکزیت پیج رنک
[('IAU ', 0.09131715448812476),
 ('University of Tehran', 0.08584797462609024),
 ('Tehran University of Medical Science', 0.07030726624696444),
 ('Shahid Beheshti University of Medical Sciences and Health Services',
  0.025862294192769166),
 ('Ferdowsi University of Mashhad', 0.016764223714683755)]


2.png

In [222]:
import seaborn as sns
In [252]:
centrality_measures = {
    'degree': degCent,
    'betweenness': betCent,
    'closeness': closeCent,
    'page Rank' : pageCent
}
centrality = pd.DataFrame(centrality_measures)

جدول مرکزیت ها

In [251]:
centrality.head()
Out[251]:
degree betweenness closeness page Rank
Academic Center for Education, Culture and Research 0.010989 0.000000 0.331512 0.003437
Ahvaz Jundishapur University of Medical Sciences 0.016484 0.003333 0.346667 0.004557
Alborza University of Medical Science 0.010989 0.000000 0.330309 0.003160
Aliabad Katoul IAU 0.010989 0.010989 0.327338 0.004216
Allameh Tabataba'i University 0.010989 0.000000 0.392241 0.003155

پیرپلات، نوعی نمودار توزیعی است که اساساً به رسم یک نمودار مشترک برای کلیه ی ترکیبات ممکن ستون های عددی و بولی در دیتاست شما می پردازد

ما فقط باید دیتافریم خود را به عنوان پارامتربه تابع پیرپلات ارسال کنیم

با استفاده از این نمودار می توان گفت که آیا داده ها بطور عادی توزیع می شود یا خیر

In [230]:
sns.pairplot(centrality)
Out[230]:
<seaborn.axisgrid.PairGrid at 0x13aba7f0>

پیش بینی پیوند

در آینده احتمال دارد کدام یک از دو دانشگاه با هم ارتباط برقرار کنند؟

روش جاکارد تعداد همسایه های مشترک دو گره / تعداد کل همسایه هاشون

In [219]:
#یک تابع تعریف می کنیم که محتمل ترین پیوند های پیشبینی شده رو  برای ما چاپ می کنه
def topSimLink(pred, n = 5):
    pred_dict={}
    #s = source, t = target
    for s,t, p in pred:
        pred_dict[(s,t)] = p
    # دیکشنری به دست آمده رو  مرتب می کنیم تا ببینیم احتمال تشکیل کدام یک از لینک ها در شبکه بیشتره
    pprint( sorted(pred_dict.items(), key = lambda x:x[1], reverse=True)[:n]    )

# تابع زیر ضریب ژاکارد برای همه  جفت گره هایی در شبکه که با هم متصل نیستند رو محاسبه می کنه
jaccard = nx.jaccard_coefficient(g)
# تبدیل به دیکشنری می کنم

topSimLink(jaccard)
[(('University of Kurdistan ', 'University of Kashan'), 1.0),
 (('University of Kurdistan ', 'University of Qom'), 1.0),
 (('University of Kurdistan ', 'Shahid Beheshti University'), 1.0),
 (('University of Kurdistan ', 'IAU Tehran North Branch '), 1.0),
 (('University of Kurdistan ', 'Malek-Ashtar University of Technology'), 1.0)]

روش اتصال ترجیحی (Preferential Attachment)

توضیح مدل: گره هایی که همسایه های بیشتری دارند در آینده نیز به گره های بیشتری متصل می شوند

#

ایده: ثروتمندان ثروتمندتر می شوند

#

ایده اصلی: اگر دو گره دارای درجه بالایی باشند احتمال بالاتری وجود دارد که به هم متصل شوند

#

معیار: ضرب درجه دو گره با هم

In [221]:
prefAtt = nx.preferential_attachment(g)
topSimLink(prefAtt)
[(('Tehran University of Medical Science', 'IAU '), 3366),
 (('IAU ',
   'Shahid Beheshti University of Medical Sciences and Health Services'),
  1254),
 (('University of Tehran',
   'Shahid Beheshti University of Medical Sciences and Health Services'),
  1197),
 (('IAU ', 'Tarbiat Modares university'), 792),
 (('University of Tehran', 'Tarbiat Modares university'), 756)]

نصب کتابخانه لُوین به منظور تشخیص اجتماع

In [1]:
# برای نصب این کتابخونه می تونید کد زیر رو اجرا کنید
!pip install python-louvain
Collecting python-louvain
  Downloading https://files.pythonhosted.org/packages/96/b2/c74bb9023c8d4bf94f5049e3d705b3064c6f180a38d772055d20ba5a8d06/python-louvain-0.13.tar.gz
Requirement already satisfied: networkx in c:\programdata\anaconda3\lib\site-packages (from python-louvain) (2.2)
Requirement already satisfied: decorator>=4.3.0 in c:\programdata\anaconda3\lib\site-packages (from networkx->python-louvain) (4.4.0)
Building wheels for collected packages: python-louvain
  Building wheel for python-louvain (setup.py): started
  Building wheel for python-louvain (setup.py): finished with status 'done'
  Stored in directory: C:\Users\Administrator\AppData\Local\pip\Cache\wheels\f9\74\a9\14f051b00dddd46d71529db15507796da13a43ee2c0bc39def
Successfully built python-louvain
Installing collected packages: python-louvain
Successfully installed python-louvain-0.13

6.jpg

معیار ماژولاریتی

الگوریتم لوین

4.png

تشریح عملکرد الگوریتم

5.png

هر تکرار از دو فاز تشکیل شده است

فاز اول) بیشینه کردن پیمانگی به طور محلی با جابجایی رئوس در خوشه‌ها

فاز دوم) ساخت خوشه‌های بدست آمده در فاز قبلی و تولید شبکه جدید

دوفاز به طور متناوب تکرار می‌شوند تا جایی که دیگر افزایشی در پیمانگی رخ ندهد.

7.jpg

In [ ]:
 

تشخیص اجتماع

In [0]:
#first compute the best partition
%matplotlib inline
partition = community.best_partition(g)

#drawing
size = float(len(set(partition.values())))
pos = nx.spring_layout(g)
count = 0.
for com in set(partition.values()) :
    count = count + 1.
    list_nodes = [nodes for nodes in partition.keys()
                                if partition[nodes] == com]
    nx.draw_networkx_nodes(g, pos, list_nodes, node_size = 40,
                                node_color = str(count / size))

#plt.figure(figsize=(12,12))
#plt.figure(figsize=(20,15))
#plt.figure(3,figsize=(12,12))
nx.draw_networkx_edges(g, pos, alpha=0.5)
plt.savefig("d:Community1.png")
plt.show()

نمایش الگوهای مصورسازی در نتورک ایکس

In [188]:
[patterns for patterns in nx.__dir__() if patterns.endswith('_layout')]
Out[188]:
['bipartite_layout',
 'circular_layout',
 'kamada_kawai_layout',
 'random_layout',
 'rescale_layout',
 'shell_layout',
 'spring_layout',
 'spectral_layout',
 'fruchterman_reingold_layout']

مصورسازی اولیه گراف

In [214]:
plt.figure(figsize=(22, 22))

plt.savefig("Viz1.png")
nx.draw(g,node_color='blue')
plt.title('Graph Representation', size=15)
plt.show()
C:\ProgramData\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):

مصورسازی هیستوگرام توزیع درجات گراف در شبکه و تشخیص اجتماع

In [44]:
degree_sequence = sorted([d for n, d in g.degree()], reverse=True)  # degree sequence
# print "Degree sequence", degree_sequence
degreeCount = collections.Counter(degree_sequence)
deg, cnt = zip(*degreeCount.items())

fig, ax = plt.subplots()
plt.bar(deg, cnt, width=0.80, color='b')

plt.title("Degree Histogram")
plt.ylabel("Count")
plt.xlabel("Degree")
ax.set_xticks([d + 0.4 for d in deg])
ax.set_xticklabels(deg)

# draw graph in inset
plt.axes([0.4, 0.4, 0.5, 0.5])
#Gcc = sorted(nx.connected_component_subgraphs(g), key=len, reverse=True)[0]
Gcc = sorted(nx.connected_components(g), key=len, reverse=True)[0]
pos = nx.spring_layout(g)

plt.axis('off')
import matplotlib.pyplot as plt
plt.figure(figsize=(20,15))
nx.draw_networkx_nodes(g, pos, node_size= 250, alpha=0.9)
nx.draw_networkx_edges(g, pos, alpha=0.7)
#plt.figure(figsize=(20,10))
plt.savefig("Hist.png")
plt.tight_layout()
plt.show()

مصورسازی دوار گراف

In [196]:
pos = nx.circular_layout(g)
betCent = nx.betweenness_centrality(g, normalized=True, endpoints=True)
node_color = [-10 * g.degree(v) for v in g] #node color refers to degree centrality
node_size =  [v * 1900 for v in betCent.values()] #node size refers to betweenness centrality
plt.figure(figsize=(14,14))
nx.draw_networkx(g, pos=pos,
                 node_color=node_color,
                 node_size=node_size, with_labels = True, font_size = 6)

plt.savefig("graph_Circular.png")

plt.axis('off')
plt.title('Collaboration Network of Notable IR Universities base on Most Communication', size = 10)
plt.show('Graph')
C:\ProgramData\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):

مصورسازی گراف با محوریت نمایش گره ها با بیشترین مرکزیت بینابینی

In [197]:
pos = nx.spring_layout(g)
betCent = nx.betweenness_centrality(g, normalized=True, endpoints=True)
node_color = [-10 * g.degree(v) for v in g] #node color refers to degree centrality
node_size =  [v * 1900 for v in betCent.values()] #node size refers to betweenness centrality
plt.figure(figsize=(14,14))
nx.draw_networkx(g, pos=pos,
                 node_color=node_color,
                 node_size=node_size, with_labels = True, font_size = 6)


plt.savefig("graph_Spring_Bet.png")

plt.axis('off')
plt.title('Collaboration Network of Notable IR Universities base on Most Communication', size = 10)
plt.show('Graph')
C:\ProgramData\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):
In [298]:
poses = [nx.spectral_layout(g),nx.random_layout(g),nx.spring_layout(g), nx.fruchterman_reingold_layout(g)]
titles = ['spectral_layout','random_layout','fruchterman_reingold_layout','spring_layout']
fig = plt.figure(figsize = (20,20))
for i in range(0,4):
    fig.add_subplot(2,2,i+1)
    node_colors = [ g.degree(n)*10 for n in g ]
    nx.draw(g, pos = poses[i], node_color='green')
    plt.title(titles[i],fontsize=20)
    plt.axis('off')
C:\ProgramData\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):
C:\ProgramData\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):
C:\ProgramData\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):
C:\ProgramData\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):
In [59]:
a = nx.to_numpy_matrix(g, dtype=int)
a = np.asarray(a)

def plotDist(a):
    
    f, ax = plt.subplots(2, 2, figsize=(8,8))
    ax[0, 0].imshow(a, cmap = 'Greys', interpolation = 'None')
    ax[0, 0].set_title('Adjacency Matrix')
    
    '''D = np.corrcoef(a)
    ax[1, 0].imshow(D, cmap = 'Greys', interpolation = 'None')
    ax[1, 0].set_title('Correlation coeff.')'''
    
    '''dVec = spt.distance.pdist(a, metric = 'euclidean')
    D = spt.distance.squareform(dVec)
    ax[0, 1].imshow(D, cmap = 'Greys', interpolation = 'None')
    ax[0, 1].set_title('Euclidean Dist.')'''
    
    '''dVec = spt.distance.pdist(a, metric = 'cosine')
    D = spt.distance.squareform(dVec)
    ax[1, 1].imshow(D, cmap = 'Greys', interpolation = 'None')
    ax[1, 1].set_title('Cosine Dist.')'''
    
plt.savefig("d:Graph Adj Matrix.png")
plotDist(a)
<Figure size 432x288 with 0 Axes>
In [ ]:
'''import community
def detectCommunities(g):
 
    parts = community.best_partition(g)
    values = [parts.get(node) for node in g.nodes()]

    plt.axis("off")
    nx.draw_networkx(g)
    plt.savefig("detectCommunities.png")'''
In [156]:
#برای اضافه کردن این کتابخونه به برنامه منتها باید از دستور زیر استفاده کنید
import community
partition  = community.best_partition(g)

#مشخصه اجتماع هر کدوم از گره ها رو به اون گره اضافه می کنیم
nx.set_node_attributes(g,partition,'community')
nx.set_node_attributes(g,node_colors,'colors')
#گراف رو رسم می کنیم
from seaborn import color_palette
palette = sns.color_palette("colorblind", 10)
palette = list(palette)    
plt.figure(figsize = (25,15))
pos = nx.kamada_kawai_layout(g)
# براساس اجتماع هر گره یک  رنگ به آن تخصیص می دهیم
node_colors = [ palette[i[1]] for i in g.nodes(data = 'community')]
for com in set(partition.values()) :

    nx.draw_networkx_nodes(g, pos, node_size = 200,
                                node_color = node_colors)
nx.draw_networkx_edges(g, pos, alpha=0.5)

plt.savefig('Community Detection_coloral.png')
plt.axis('off')
plt.show()
C:\ProgramData\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):

Untitled.png

کدام دانشگاه متعلق به کدام اجتماع است؟

In [206]:
def who_to_which(n):
    for i,j in partition.items():
        if partition[i] == n :
            print(i)
who_to_which(4)     
Iran University of Science and Technology
University of Applied Science and Technology
Ferdowsi University of Mashhad
Mashhad University of Medical Sciences
University of Sistan and Baluchestan
Uniersity of Tehran
University of Birjand
Birjand University of Technology
Birjand University of Medical Sciences
Arak University 
Arak University of Technology
Imam Reza International University
Damghan University
University of Bojnord
University of Neyshabur
Neyshabur University of Medical Sciences 
University of Sabzevar
IAU , Zahedan Branch
Quchan University of Technology
Sadjad Institute of Higher Education
Deakin University
Torbat Heydarieh University of Medical Sciences 

The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter.

In [163]:
from pprint import pprint

#یک تابع تعریف می کنیم که محتمل ترین لینک های پیشبینی شده رو  برای ما چاپ می کنه
def n_most_likely_links(pred, n = 10):
    pred_dict={}
    for s,t, p in pred:
        pred_dict[(s,t)] = p
    # دیکشنری به دست آمده رو  مرتب می کنیم تا ببینیم احتمال تشکیل کدام یک از لینک ها در شبکه بیشتره
    pprint( sorted(pred_dict.items(), key = lambda x:x[1], reverse=True)[:n]    )

# تابع زیر ضریب ژاکارد برای همه  جفت گره هایی در شبکه که با هم متصل نیستند رو محاسبه می کنه
pred_jaccard = nx.jaccard_coefficient(g)
# تبدیل به دیکشنری می کنم

n_most_likely_links(pred_jaccard)
[(('University of Kurdistan ', 'University of Kashan'), 1.0),
 (('University of Kurdistan ', 'University of Qom'), 1.0),
 (('University of Kurdistan ', 'Shahid Beheshti University'), 1.0),
 (('University of Kurdistan ', 'IAU Tehran North Branch '), 1.0),
 (('University of Kurdistan ', 'Malek-Ashtar University of Technology'), 1.0),
 (('University of Kurdistan ', 'Alzahra University'), 1.0),
 (('University of Kurdistan ', 'IAU East Tehran Branch '), 1.0),
 (('University of Kurdistan ', 'IAU South Tehran Branch '), 1.0),
 (('University of Kurdistan ',
   'Gorgan University of Agricultural Sciences and Natural Resources'),
  1.0),
 (('University of Kurdistan ', 'Imam Hossein University'), 1.0)]
In [0]:
pred_r_a = nx.resource_allocation_index(g) 
n_most_likely_links(pred_r_a)
[(('University of Tehran', 'Tarbiat Modares university'), 2.519607843137255),
 (('Tabriz University of Medical Sciences',
   'Tehran University of Medical Science'),
  1.5158730158730158),
 (('Tehran University of Medical Science', 'IAU '), 1.5158730158730158),
 (('IAU ', 'University of Tabriz'), 1.3492063492063493),
 (('IAU ', 'Tarbiat Modares university'), 1.1666666666666665),
 (('IAU ', 'Shiraz University'), 0.8492063492063492),
 (('University of Sistan and Baluchestan', 'University of Birjand'),
  0.5909090909090909),
 (('Ferdowsi University of Mashhad', 'Birjand University of Technology'),
  0.5833333333333333),
 (('University of Tehran', 'Arak University of Technology'),
  0.5333333333333333),
 (('Arak University ', 'Iran University of Science and Technology'),
  0.5158730158730158)]
In [0]:
pred_pr_at = nx.preferential_attachment(g) 
n_most_likely_links(pred_pr_at) 
[(('Tehran University of Medical Science', 'IAU '), 3366),
 (('IAU ',
   'Shahid Beheshti University of Medical Sciences and Health Services'),
  1254),
 (('University of Tehran',
   'Shahid Beheshti University of Medical Sciences and Health Services'),
  1197),
 (('IAU ', 'Tarbiat Modares university'), 792),
 (('University of Tehran', 'Tarbiat Modares university'), 756),
 (('Ferdowsi University of Mashhad', 'IAU '), 726),
 (('IAU ', 'University of Tabriz'), 726),
 (('IAU ', 'Iran University of Medical Sciences'), 660),
 (('University of Tehran', 'Iran University of Medical Sciences'), 630),
 (('Tehran University of Medical Science', 'University of Tabriz'), 561)]
In [164]:
!pip install nxviz
Collecting nxviz
  Downloading https://files.pythonhosted.org/packages/26/23/43d8bcd236e69cba845ac498501fcca35876f878aab481882c17a6fa55d2/nxviz-0.6.1-py3-none-any.whl
Requirement already satisfied: networkx==2.2 in c:\programdata\anaconda3\lib\site-packages (from nxviz) (2.2)
Requirement already satisfied: matplotlib==3.0.3 in c:\programdata\anaconda3\lib\site-packages (from nxviz) (3.0.3)
Collecting hypothesis==4.14.0 (from nxviz)
  Downloading https://files.pythonhosted.org/packages/28/d6/0e0452d26ecf9c8f704730953a0cad4abf39b1d56472ff5bb77dbbfa9b18/hypothesis-4.14.0-py3-none-any.whl (254kB)
Requirement already satisfied: numpy==1.16.2 in c:\programdata\anaconda3\lib\site-packages (from nxviz) (1.16.2)
Collecting sphinxcontrib-fulltoc==1.2.0 (from nxviz)
  Downloading https://files.pythonhosted.org/packages/8e/a6/d1297db9b75650681e5429e92e13df139ee6b64303ff1b2eea4ebd32c0a9/sphinxcontrib-fulltoc-1.2.0.tar.gz
Requirement already satisfied: seaborn==0.9.0 in c:\programdata\anaconda3\lib\site-packages (from nxviz) (0.9.0)
Requirement already satisfied: more-itertools==6.0.0 in c:\programdata\anaconda3\lib\site-packages (from nxviz) (6.0.0)
Requirement already satisfied: cryptography==2.6.1 in c:\programdata\anaconda3\lib\site-packages (from nxviz) (2.6.1)
Requirement already satisfied: PyYAML==5.1 in c:\programdata\anaconda3\lib\site-packages (from nxviz) (5.1)
Collecting palettable==3.1.1 (from nxviz)
  Downloading https://files.pythonhosted.org/packages/56/8a/84537c0354f0d1f03bf644b71bf8e0a50db9c1294181905721a5f3efbf66/palettable-3.1.1-py2.py3-none-any.whl (77kB)
Requirement already satisfied: setuptools==40.8.0 in c:\programdata\anaconda3\lib\site-packages (from nxviz) (40.8.0)
Requirement already satisfied: pandas==0.24.2 in c:\programdata\anaconda3\lib\site-packages (from nxviz) (0.24.2)
Requirement already satisfied: pytest==4.3.1 in c:\programdata\anaconda3\lib\site-packages (from nxviz) (4.3.1)
Requirement already satisfied: decorator>=4.3.0 in c:\programdata\anaconda3\lib\site-packages (from networkx==2.2->nxviz) (4.4.0)
Requirement already satisfied: cycler>=0.10 in c:\programdata\anaconda3\lib\site-packages (from matplotlib==3.0.3->nxviz) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\programdata\anaconda3\lib\site-packages (from matplotlib==3.0.3->nxviz) (1.0.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\programdata\anaconda3\lib\site-packages (from matplotlib==3.0.3->nxviz) (2.3.1)
Requirement already satisfied: python-dateutil>=2.1 in c:\programdata\anaconda3\lib\site-packages (from matplotlib==3.0.3->nxviz) (2.8.0)
Requirement already satisfied: attrs>=16.0.0 in c:\programdata\anaconda3\lib\site-packages (from hypothesis==4.14.0->nxviz) (19.1.0)
Requirement already satisfied: scipy>=0.14.0 in c:\programdata\anaconda3\lib\site-packages (from seaborn==0.9.0->nxviz) (1.2.1)
Requirement already satisfied: cffi!=1.11.3,>=1.8 in c:\programdata\anaconda3\lib\site-packages (from cryptography==2.6.1->nxviz) (1.12.2)
Requirement already satisfied: asn1crypto>=0.21.0 in c:\programdata\anaconda3\lib\site-packages (from cryptography==2.6.1->nxviz) (0.24.0)
Requirement already satisfied: six>=1.4.1 in c:\programdata\anaconda3\lib\site-packages (from cryptography==2.6.1->nxviz) (1.12.0)
Requirement already satisfied: pytz>=2011k in c:\programdata\anaconda3\lib\site-packages (from pandas==0.24.2->nxviz) (2018.9)
Requirement already satisfied: py>=1.5.0 in c:\programdata\anaconda3\lib\site-packages (from pytest==4.3.1->nxviz) (1.8.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\programdata\anaconda3\lib\site-packages (from pytest==4.3.1->nxviz) (1.3.0)
Requirement already satisfied: pluggy>=0.7 in c:\programdata\anaconda3\lib\site-packages (from pytest==4.3.1->nxviz) (0.9.0)
Requirement already satisfied: colorama in c:\programdata\anaconda3\lib\site-packages (from pytest==4.3.1->nxviz) (0.4.1)
Requirement already satisfied: pycparser in c:\programdata\anaconda3\lib\site-packages (from cffi!=1.11.3,>=1.8->cryptography==2.6.1->nxviz) (2.19)
Building wheels for collected packages: sphinxcontrib-fulltoc
  Building wheel for sphinxcontrib-fulltoc (setup.py): started
  Building wheel for sphinxcontrib-fulltoc (setup.py): finished with status 'done'
  Stored in directory: C:\Users\Administrator\AppData\Local\pip\Cache\wheels\74\30\b2\f2da9acb18f37b571f3c17aa03ee6f1ac555514d99d76b9af0
Successfully built sphinxcontrib-fulltoc
Installing collected packages: hypothesis, sphinxcontrib-fulltoc, palettable, nxviz
Successfully installed hypothesis-4.14.0 nxviz-0.6.1 palettable-3.1.1 sphinxcontrib-fulltoc-1.2.0
In [165]:
from nxviz import MatrixPlot
from nxviz import ArcPlot
from nxviz import CircosPlot

m = MatrixPlot(g ,node_grouping='community')
plt.figure(figsize = (22,12))
m.draw()
plt.show()
<Figure size 1584x864 with 0 Axes>
In [250]:
#رنگ و گروه هر کدام از گره در نمودار را بر اساس مشخصه اجتماع آن گره قرار می دهیم
A = ArcPlot(g, nodes_size= 22, node_color='community',node_grouping='community')
plt.figure(figsize = (22,12))
A.draw()
plt.show()
<Figure size 1584x864 with 0 Axes>
In [170]:
plt.figure(figsize = (12,8))
C= CircosPlot(g ,node_color='community',nodes_grouping='community')
plt.figure(figsize = (22,12))

C.draw()
plt.show()
<Figure size 864x576 with 0 Axes>
<Figure size 1584x864 with 0 Axes>
In [173]:
plt.figure(figsize = (20,15))
pos = nx.fruchterman_reingold_layout(g)
# رنگ گره ها رو بر اساس درجه هر گره تعیین می کنیم. هرچه پر رنگ تر درجه بیشتر
node_colors = [ g.degree(n) for n in g ]
#اندازه گره هایی که رسم می شوند رو بر اساس مرکزیت بینابینی قرار می دیم
node_size = [20000*nx.betweenness_centrality(g)[n] for n in g]
nx.draw_networkx(g,pos = pos,node_color=node_colors, node_size=node_size,
                 edge_color='1',alpha = 1,width = 0.8, cmap = plt.cm.Blues_r ,label_size = 12, with_labels = False)

plt.savefig('Important_Nodes.jpg', dpi= 1000)

plt.axis('off')
plt.show()
C:\ProgramData\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):
In [180]:
fb_degrees = list(dict(g.degree()).values())
plt.hist(fb_degrees, bins = 30)
#plt.axis('off')
plt.grid(False)
plt.show()

GEXF Designed to be a standard exchange format for graphs (Gephi) nx.read_gexf nx.write_gexf

In [295]:
nx.write_gexf(g, 'gephi.gexf')